Decorate={}
DecorateFunc={}
DecorateFunc.__index = DecorateFunc
setmetatable(Decorate, DecorateFunc)

DecorateFunc.getAllDecorates = function(self)
  local seq = {}
  for k,v in pairs(UserData.userinfo.host.store.decor.u) do
    seq[k] = v
  end
  for k,v in pairs(UserData.userinfo.host.store.decor.b) do
    seq[k] = v
  end
  for k,v in pairs(UserData.userinfo.host.store.decor.m) do
    seq[k] = v
  end
  return seq
end

DecorateFunc.calcLevel = function(self, itemIndex)
  local XZLevelIndex  = {green=1, blue=11, purple=21, orange=31}
  local DecorateLevel = UserData.config.models.DecorateLevel
  if self.itemInfo.x==nil then
    return 1, 0, DecorateLevel[2].xp
  end
  local begin = XZLevelIndex[self.itemConfig.quality]
  local tmp = 0
  for i=begin, begin+9 do
    tmp = i 
    if self.itemInfo.x < DecorateLevel[i].xp then
      break
    end
  end
  print("DecorateFunc.calcLevel", begin, tmp)
  return DecorateLevel[tmp-1].level, self.itemInfo.x, DecorateLevel[tmp].xp
end

DecorateFunc.create = function(self, itemIndex)
  local item = {}
  setmetatable(item, DecorateFunc)
  item.index = itemIndex
  item.itemInfo = self:getAllDecorates()[tostring(itemIndex)]
  item.itemConfig = UserData.config.models.Decorate[item.itemInfo.d] 
  --item.itemLevel = item:getDecorateLevel(item.itemInfo.i) 
  return item
end

DecorateFunc.createTip = function(self, target)
  local tipLayer = CCBReader:load("DecorateProperty.ccbi")
  addSpriteFromBK(tipLayer, 1001, nameFromPath(self.itemConfig.asset))
  
  TypeCast(tipLayer:getChildByTag(1010), "CCLabelTTF"):setString(self.itemConfig.name)
  local l, xp1, xp2 = self:calcLevel() 
  TypeCast(tipLayer:getChildByTag(1011), "CCLabelTTF"):setString(string.format("Lv%d", l))
  TypeCast(tipLayer:getChildByTag(1012), "CCLabelTTF"):setString(string.format("%d/%d", xp1, xp2))
  local tmp = string.format("level%d", l)
  local boost = self.itemConfig[tmp]
  local desc = Equip.EquipAttrDesc[self.itemConfig.effect]
  TypeCast(tipLayer:getChildByTag(1013), "CCLabelTTF"):setString(string.format("%s +%d", desc, boost))
  TypeCast(tipLayer:getChildByTag(1014), "CCLabelTTF"):setString(string.format("%d", self.itemConfig.saleCash))
  return tipLayer
end
